package com.bcgtgjyb.huanwen.customview.mylibrary.star;

import com.bcgtgjyb.huanwen.customview.mylibrary.ResourceTable;
import com.bcgtgjyb.huanwen.customview.mylibrary.tool.ScreenUtil;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by bigwen on 2016/4/23.
 */
public class StarCircle extends DirectionalLayout implements Component.EstimateSizeListener, Component.BindStateChangedListener {
    private static final int SIXTEEN = 16;
    private static final int TWO = 2;
    private static final int EIGHT = 8;
    private static final int THIRTY = 30;
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Context mContext;
    private StarView starView1;
    private StarView starView2;
    private int viewWidth;
    private int viewHeight;
    private Thread thread;
    private boolean isPlay = false;
    private float degree = 0;
    private String TAG = StarCircle.class.getName();
    private int padding;
    private double circleDegree;
    private int circleX,circleY,circleR;
    private int[] coordinate1,coordinate2;
    /**
     * StarCircle 构造方法
     *
     * @param context 内容
     */

    public StarCircle(Context context) {
        super(context);
        mContext = context;
        init();
    }

    /**
     * StarCircle 构造方法
     *
     * @param context 内容
     * @param attrs 属性
     */
    public StarCircle(Context context, AttrSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        setEstimateSizeListener(this);
        setBindStateChangedListener(this);
        LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_star_circle, this, true);
        starView1 = (StarView) findComponentById(ResourceTable.Id_star_view_1);
        starView2 = (StarView) findComponentById(ResourceTable.Id_star_view_2);
        padding = ScreenUtil.dip2px(mContext, EIGHT);
        initThread();
    }

    private void initThread() {
        if (thread != null && thread.isAlive()) {
            return;
        }
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isPlay) {
                    degree += 0.1f;
                    setXy(degree);
                    try {
                        Thread.sleep(SIXTEEN);
                    } catch (InterruptedException e) {
                    }
                    eventHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            invalidate();
                        }
                    });
                }
            }
        });
        isPlay = true;
        thread.start();
    }

    private void setXy(float degreeX) {
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                coordinate1 = caculateDegree(degreeX);
                coordinate2 = caculateDegree(degreeX);
                starView1.setTranslationX(coordinate1[0]);
                starView1.setTranslationY(coordinate1[1]);
                starView2.setTranslationX(coordinate2[0]);
                starView2.setTranslationY(coordinate2[1]);
            }
        });
    }

    private int[] caculateDegree(float dd) {
        circleDegree = dd / Math.PI;
        circleX = viewWidth / TWO - padding;
        circleY = viewHeight / TWO - padding;
        circleR = viewWidth / TWO;
        int[] xy = new int[TWO];
        xy[0] = (int) (circleX + circleR * Math.cos(circleDegree));
        xy[1] = (int) (circleY + circleR * Math.sin(circleDegree));
        return xy;
    }

    /**
     * 开始
     *
     */
    public void start() {
        isPlay = true;
        initThread();
    }

    /**
     * 结束
     *
     */
    public void stop() {
        isPlay = false;
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        start();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        stop();
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        viewWidth = getWidth() - ScreenUtil.dip2px(mContext, THIRTY);
        viewHeight = getHeight() - ScreenUtil.dip2px(mContext, THIRTY);
        return false;
    }
}
